#!/usr/bin/env python

from banner_class import Banner

def init ( l, admin = 0 ):
	from liwe import parts

	l.layman.add_js ( "modules/banner/js/banner.js" )
	l.layman.add_js ( "modules/banner/js/banner.templates.js" )

	l.pyhp.log.debug ( "ADMIN: %s" % admin )

	if admin and int ( admin ):
		parts.add ( l.layman, [ "form" ] )

		l.layman.add_js ( "modules/banner/js/banner.admin.js" )
		l.layman.add_js ( "modules/banner/js/banner.admin.templates.js" )
   		l.layman.add_js_code ( "banner.admin.init ();" )
		l.layman.add_css ( "modules/banner/gfx/banner.admin.css" )

	return ''



def show ( liwe, container = None ):
	"""
	:INPUT:

		- container:		SAFE_DB_STRING
	"""

	b = Banner ( liwe )
	banner = b.banner_fetch ( container )
	if not banner: return "NO BANNER"

	id_cont = banner [ "id_container" ]

	if not banner.get ( "media" ): return "NO MEDIA"
	media = banner [ "media" ]

	html = media [ "html" ]
	if media [ "kind" ] == "image":
		html = html % { "_size": "orig", "_ext" : media [ 'ext' ] }

	if not container: container = ""

	dest = "banner_cnt:%s" % id_cont

	if banner [ "view_time" ]:
		html += '<script type="text/javascript">banner.timeout_banner(\'%s\',\'%s\')</script>' % ( dest, container )

	cnt = '<div class="banner_cnt" id="%s">%s</div>' % ( dest, html )

	return cnt

